<?php
namespace app\index\controller;
use app\index\model\Base as bs;
use think\Db;
class Index extends Base
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }


    /**
     *显示所有的赛事活动
     * @param
     * @return
     */
    public function listActivity(){
        $type = input('type');
        $page = input('page',1,'intval');
        $page_size = input('page_size',10,'intval');
        $where = '';
        if($type){
            $where = ['type' => $type];
        }
        $data = bs::project('article','id,title,time,article,img',$where,$page,$page_size);
        $this->apiReturn(200,'',$data);
    }

    public function getActivityParticular(){
        $id = input('id');//文章id
        if(!$id){
            $this->apiReturn(303,'lack id');
        }
        $data = bs::particular('article','id,title,time,article,img',$id);
        $this->apiReturn(200,'',$data);
    }

    /**
     *显示所有用户识别
     * @param
     * @return
     */
    public function getListTrader(){
        $page = input('page',1,'intval');
        $page_size = input('page_size',10,'intval');
        $open_id = input('open_id');
        $expiration = bs::getKey('user',['open_id'=>$open_id],'expiration');
        $name = input('name');
        $where = [];

        if($name){
            $where = ['name'=>['like','%'.$name.'%']];
        }
        $field = 'id,account,name,profit_loss,profit,loss,victory,frequency';
        $total = bs::total('apply','');
        $total = $total > 200 ? $total : 200;
        if($open_id && $expiration > time()){
            $data = bs::project('trader',$field,$where,$page,$page_size,['victory'=>'desc']);
            $this->apiReturn(200,'',$data,$total);
        }else{
            $data = bs::project('trader',$field,'',1,3,['victory'=>'desc']);
            $this->apiReturn(200,'',$data,$total);
        }
    }


    /**
     *支付模板列表
     * @param
     * @return
     */
    public function listPayTemplate(){
        $data = bs::projects('pay_template','id,name,number_day,money,img,introduction','');
        $total = bs::total('pay_template','');
        $this->apiReturn(200,'',$data,$total);
    }


    /**
     *得到交易报表
     * @param
     * @return
     */
    public function getListTransaction(){
        $page = input('page',1,'intval');
        $page_size = input('page_size',10,'intval');
        $field = 'id,name,agreement_name,agreement_date,more_number,empty_number,profit_loss';
        $data = bs::project('transaction',$field,'',$page,$page_size);
        $this->apiReturn(200,'',$data);
    }


    /**
     *当日成交列表
     * @param
     * @return
     */
    public function getListDayTrading(){
        $page = input('page',1,'intval');
        $page_size = input('page_size',10,'intval');
        $open_id = input('open_id');
        if(!$open_id){
            $this->apiReturn(303,'lack open_id');
        }
        $expiration = bs::getKey('user',['open_id'=>$open_id],'expiration');
        if(time()>$expiration){
            $this->apiReturn(101,'101');
        }
        $name = input('name');
        $where = [];
        if($name){
            $where = ['name'=>['like','%'.$name.'%']];
        }
        $tradin = new bs();
        $field = 'id,name,contract,open_price,flat_price,fixture_date,fixture_quantity,more_empty,profit_loss,fixture_type';
        $data = $tradin->getListTrading($field,$where,$page,$page_size);
        foreach ($data as $k=>$v){
            $data[$k]['fixture_date'] = date('Y-m-d H:i:s',$v['fixture_date']);
        }
        $this->apiReturn(200,'',$data);
    }


    /**
     *报名申请
     * @param
     * @return
     */
     public function apply(){
         $value = I('name|id_card|phone|type|nickname');
         foreach ($value as $k=>$v){
             if(!$v){
                 $this->apiReturn(303,'lack '.$k);
             }
         }
         if($value['type'] ==3){
             $location = input('location');
             if(!$location){
                 $this->apiReturn(303,'lack location');
             }
             $value['location'] = $location;
         }
         $value['add_time'] = time();
         $data = bs::addProject('apply',$value);
         $this->apiReturn(200,'',$data);

     }

    /**
     *持仓分布列表
     * @param
     * @return
     */
    public function getListDistribute(){
        $open_id = input('open_id');
        if(!$open_id){
            $this->apiReturn(303,'lack open_id');
        }
        $expiration = bs::getKey('user',['open_id'=>$open_id],'expiration');
        if(time()>$expiration){
            $this->apiReturn(101,'101');
        }
        $data = bs::project('distribute','id,contract,contract_chinese,polycell,empty,update_time','');
        foreach ($data as $k=>$v){
            $data[$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
        }
        $this->apiReturn(200,'',$data);
    }

    /**
     *显示界面
     * @param
     * @return
     */
    public function getSurface(){
        $data = bs::project('surface','id,img,title,content');
        $this->apiReturn(200,'',$data);
    }

    /**
     * 字段表
     * @param stirng
     * @return string
     */
	public function getListField(){
        $data = bs::project('field','id,name,english_name,type','','1','100');
        $this->apiReturn(200,'',$data);
    }


    /**
     *图片列表
     * @param
     * @return
     */
    public function getListImg(){
        $page = input('page','1','intval');
        $page_size = input('page_size',10,'intval');
//        $name = input();
        $data = bs::project('other','id,name,img_url,type','',$page,$page_size);
        $total = bs::total('other','');
        $this->apiReturn(200,'',$data,$total);
    }

    /**
     * 得到协议
     * @param stirng
     * @return string
     */
    public function getAgreement(){
        $data = bs::project('agreement','id,content','');
        $this->apiReturn(200,'',$data);
    }

    public function getListData(){
        $data = Db::query('select `id`,`name`,`victory` from wr_trader GROUP BY `name` ORDER BY `victory` desc limit 30;');
        $this->apiReturn(200,'',$data);
    }

}

